/*function normal(data:Object):Object
{
	var content:String="";
	content+="<portlet name='chapterListContentContainer' x='5' y='25' layout='area:150:640' >";
	data.objects.reverse();
	for (var a=0;a<data.objects.length;a++)
	{
		content+=getChapterItem(data.objects[a]);
	}
	content+="</portlet>";

	content+=appendOper(170);

	var voData:Object=new Object();
	voData.type="Template";
	voData.content=addCommonPart(content);

	return voData;
}*/

function normal(data:Object, context:Object):Object
{
	var voData:Object=new Object();
		
	
	if (context.rules==null)
	{
		voData.content=albumInfoNormal(data);
		voData.type="Template";

		Packages.log("normal.Template", "debug");
	}
	else if (context.rules=="doWrapper")
	{
		voData.content=relatedInfoNormal(data, context.chapterListNames);
		voData.type="doWrapper";

		Packages.log("normal.doWrapper", "debug");
	}

	return voData;
}

function albumInfoNormal(data:Object):String
{
	 Packages.log("albumInfoNormal", "debug");
	
	var cont:String="";

	cont+="<portlet name='chapterListContentContainer' x='5' y='25' layout='area:150:640' />";
	cont+=appendOper(170);

	cont=addCommonPart(cont, data.objects[0].name, "AlbumOperLabel_Normal");

	// Packages.log("cont: "+cont, "debug");

	return cont;
}

function relatedInfoNormal(data:Object, chapterListNames:Object):Object
{
	// Packages.log("relatedInfoNormal", "debug");
	
	var content:Object=new Object();

	var taskStatusML2:String="";
	// chapterListNames.objects.reverse();
	// ref logic 
	// condition judgement
	// extra
	// whole
	/*for (var i=0; i<data.count; i++)
	{
		taskStatusML2+=getTaskStatus(data.objects[i], chapterListNames);
	}*/
	for (var i=0; i<chapterListNames.count; i++)
	{
		taskStatusML2+=getChapterNamesByTaskStatus(chapterListNames.objects[i], data);
	}

	var ml2fileStr:String="";
	ml2fileStr+="<moonlight name='BookSpecial' template='BookSpecial-chapterListContentContainer' uri='app/content/bookPage/BookSpecial.xml'>";
	ml2fileStr+=taskStatusML2;
	ml2fileStr+="</moonlight>";

	// Packages.log("ml2fileStr: "+ml2fileStr, "debug");

	content.tasks=new Object();	// ml2 template item
	content.tasks.type="ml2ti";
	content.tasks.template="BookSpecial-chapterListContentContainer";
	content.tasks.data=ml2fileStr;

	return content;
}

function expand(data:Object, context:Object):Object
{	
	var voData:Object=new Object();
		
	if (context.rules=="doWrapper")
	{
		voData.content=relatedInfoExpand(data, context.albumDetails, context.selectedAlbumId, context.chapterListNames);
		voData.type="doWrapper";
	}
	else 
	{
		voData.content=albumListExpand(data);
		voData.type="Template";
	}

	return voData;
}

function relatedInfoExpand(data:Object, albumDetails:Object, selectedAlbumId:String, chapterListNames:Object):Object
{
	var content:Object=new Object();
	
	for (var i=0; i<albumDetails.count; i++)
	{
		if (albumDetails.objects[i].recordAlbumId==Number(selectedAlbumId))
		{
			var selectedAlbum=albumDetails.objects[i];
			content.name=selectedAlbum.name;
			content.creator=selectedAlbum["member.nickName"];
			content.createTime=selectedAlbum.createTime;
			content.members=selectedAlbum.members;
			content.scope=selectedAlbum.publicFlag;
			content.collab=selectedAlbum.collabFlag;
			content.des=selectedAlbum.intro;
			content.albumRate=selectedAlbum.albumRating;
			content.expect=selectedAlbum.expectRate;
			content.activeLevel=selectedAlbum.activeLevel;

			break;
		}
	}

	var taskStatusML2:String="";
	data.objects.reverse();
	/*for (var i=0; i<data.count; i++)
	{
		taskStatusML2+=getTaskStatus(data.objects[i], chapterListNames);
	}*/
	for (var i=0; i<chapterListNames.count; i++)
	{
		taskStatusML2+=getChapterNamesByTaskStatus(chapterListNames.objects[i], data);
	}

	var ml2fileStr:String="";
	ml2fileStr+="<moonlight name='BookSpecial' template='BookSpecial-chapterListContentContainer2' uri='app/content/bookPage/BookSpecial.xml'>";
	ml2fileStr+="<portlet name='albumChapter' linew='-1' curve='0' layout='area:160:640'>"
	ml2fileStr+=taskStatusML2;
	ml2fileStr+="</portlet></moonlight>";

	content.tasks=new Object();	// ml2 template item
	content.tasks.type="ml2ti";
	content.tasks.template="BookSpecial-chapterListContentContainer2";
	content.tasks.data=ml2fileStr;

	// Packages.log("value: "+content.tasks.data, "debug");

	return content;
}

function getChapterNamesByTaskStatus(item:Object, taskStatus:Object):String
{
	var chapterItem=new Object();
	chapterItem.chapterId=item.chapterId;
	chapterItem.name=item.name;
	chapterItem.status="0";
	
	for (var i=0; i<taskStatus.count; i++)
	{
		if (taskStatus.objects[i]["chapter.chapterId"]==item.chapterId)
		{
			chapterItem.status=taskStatus.objects[i].status;
			break;
		}
	}	

	return getChapterItem(chapterItem);
}

function getTaskStatus(item:Object, chapterListNames:Object):String
{
	var chapterItem=new Object();
	chapterItem.chapterId=item["chapter.chapterId"];
	chapterItem.status=item.status?item.status:"0";
	
	for (var i=0; i<chapterListNames.count; i++)
	{
		if (chapterListNames.objects[i].chapterId==item["chapter.chapterId"])
		{
			chapterItem.name=chapterListNames.objects[i].name;
			break;
		}
	}	

	return getChapterItem(chapterItem);
}

function albumListExpand(data:Object):String
{
	var content:String="";
	content+="<portlet name='albumListContainer' x='5' y='25'>";
	content+="<portlet name='albumListContentContainer' linew='-1' curve='0' layout='list:160:180'>"
	for (var i=0; i<data.count; i++)
	{
		content+=getAlbumItem(data.objects[i]);
	}
	content+="</portlet>";
	content+="<portlet name='albumContent' linew='-1' curve='0' height='160' width='420' x='220'>";
	content+=buildPropertyField();
	content+="</portlet></portlet>";
	content+="<portlet name='chapterListContentContainer2' x='5' y='205'>";
	//content+="<portlet name='albumChapter' linew='-1' curve='0' layout='area:160:640'/>"
	content+="</portlet>";
	content+=appendOper(380);

	content=addCommonPart(content, null, "AlbumOperLabel_Expand");

	return content;
}

function buildPropertyField():String
{
	var cont:String="";

	cont+="<textfield name='nameHint' x='5' y='0' width='80' selectable='no' html='true'>AlbumNameLabel</textfield>";
	cont+="<textfield name='name' x='85' y='0' width='160' selectable='no' html='true'/>";
	cont+="<textfield name='creatorHint' x='5' y='20' width='80' selectable='no' html='true'>CreatorLabel</textfield>";
	cont+="<textfield name='creator' x='85' y='20' width='80' selectable='no' html='true'/>";
	cont+="<textfield name='createTimeHint' x='175' y='20' width='80' selectable='no' html='true'>CreateTimeLabel</textfield>";
	cont+="<textfield name='createTime' x='255' y='20' width='160' selectable='no' html='true'/>";
	cont+="<textfield name='membersHint' x='5' y='40' width='80' selectable='no' html='true'>MembersLabel</textfield>";
	cont+="<textfield name='members' x='85' y='40' width='320' selectable='no' html='true'/>";
	cont+="<textfield name='scopeHint' x='5' y='60' width='80' selectable='no' html='true'>ScopeLabel</textfield>";
	cont+="<textfield name='scope' x='85' y='60' width='80' selectable='no' html='true'/>";
	cont+="<textfield name='collabHint' x='175' y='60' width='80' selectable='no' html='true'>CollabLabel</textfield>";
	cont+="<textfield name='collab' x='255' y='60' width='160' selectable='no' html='true'/>";
	cont+="<textfield name='desHint' x='5' y='80' width='80' selectable='no' html='true'>DescriptionLabel</textfield>";
	cont+="<textfield name='des' x='85' y='80' width='320' selectable='no' html='true' height='60'/>";
	cont+="<textfield name='albumRateHint' x='5' y='160' width='40' selectable='no' html='true'>AlbumRateLabel</textfield>";
	cont+="<textfield name='albumRate' x='45' y='160' width='50' selectable='no' html='true'/>";
	cont+="<textfield name='expectHint' x='105' y='160' width='40' selectable='no' html='true'>ExpectLabel</textfield>";
	cont+="<textfield name='expect' x='145' y='160' width='50' selectable='no' html='true'/>";
	cont+="<textfield name='activeLevelHint' x='205' y='160' width='40' selectable='no' html='true'>ActiveLevelLabel</textfield>";
	cont+="<textfield name='activeLevel' x='245' y='160' width='50' selectable='no' html='true'/>";

	return cont;
}

function getAlbumItem(item:Object):String
{
	var cont:String;
	cont="<portlet name="+"'albumItem_"+item.recordAlbumId+"' drag='no' height='25'>";
	cont+="<textfield name='albumName_"+item.recordAlbumId+"' x='5' y='0' html='true' selectable='no' width='190' height='20'>"			+item.name+"</textfield>";
	cont+="<hotspot name='aItem"+item.recordAlbumId+"' x='5' y='0' width='180' height='20' alpha='0'/>";
	cont+="<handler ins='BookSpecial-aItem"+item.recordAlbumId+"' type='onRollOver' src='lib:notes.system.events.StateFulButtonEvent.invokeRollOverEvent'/>";
	cont+="<handler ins='BookSpecial-aItem"+item.recordAlbumId+"' type='onRollOut' src='lib:notes.system.events.StateFulButtonEvent.invokeRollOutEvent'/>";
	cont+="<handler ins='BookSpecial-aItem"+item.recordAlbumId+"' type='onPress' src='lib:notes.system.events.StateFulButtonEvent.invokePressEvent'/>";
	cont+="<handler ins='BookSpecial-aItem"+item.recordAlbumId+"' type='onRelease' src='lib:notes.system.events.StateFulButtonEvent.invokeReleaseEvent'/>";
	cont+="<handler ins='BookSpecial-aItem"+item.recordAlbumId+"' type='onReleaseOutside' src='lib:notes.system.events.StateFulButtonEvent.invokeRollOutEvent'/>";
	cont+="</portlet>";

	return cont;
}

function getChapterItem(item:Object):String
{
	// TODO: condition - item.status
	var textColor;
	if (item.status=="0") textColor=0xcccccc;
	else if (item.status=="1") textColor=0x999999;
	else if (item.status=="2") textColor=0x666666;
	else if (item.status=="3") textColor=0x333333;
	else if (item.status=="4") textColor=0x000000;
	
	var cont:String;
	cont="<portlet name="+"'chapterItem_"+item.chapterId+"' drag='no' height='25'>";	
	cont+="<textfield name='chapterName_"+item.chapterId+"' x='5' y='0' html='true' selectable='no' width='200' height='20' textColor='"+textColor+"'>"
			//+Packages.system.utils.TextUtil.formatText("tfContent", item.name)+"</textfield>";
			+item.name+"</textfield>";
	cont+="<hotspot name='cItem"+item.chapterId+"' x='5' y='0' width='200' height='20' alpha='0'/>";
	cont+="<handler ins='BookSpecial-cItem"+item.chapterId+"' type='onRollOver' src='lib:notes.system.events.StateFulButtonEvent.invokeRollOverEvent'/>";
	cont+="<handler ins='BookSpecial-cItem"+item.chapterId+"' type='onRollOut' src='lib:notes.system.events.StateFulButtonEvent.invokeRollOutEvent'/>";
	cont+="<handler ins='BookSpecial-cItem"+item.chapterId+"' type='onPress' src='lib:notes.system.events.StateFulButtonEvent.invokePressEvent'/>";
	cont+="<handler ins='BookSpecial-cItem"+item.chapterId+"' type='onRelease' src='lib:notes.system.events.StateFulButtonEvent.invokeReleaseEvent'/>";
	cont+="<handler ins='BookSpecial-cItem"+item.chapterId+"' type='onReleaseOutside' src='lib:notes.system.events.StateFulButtonEvent.invokeRollOutEvent'/>";
	cont+="</portlet>";

	// Packages.log("content:"+cont, "debug");
	return cont;
}

function addCommonPart(spec:String, name:String, AlbumOperLabel:String):String
{
	var albumName=name?name:"";
	var content:String="";
	content+="<moonlight name='BookSpecial' template='BookSpecial-chapterListContainer' uri='app/content/bookPage/BookSpecial.xml'>";
	content+="<portlet name='chapterListInfo' drag='no' linew='-1' curve='0' height='90' width='650'>";
	content+="<textfield name='albumNameLabel' html='true'>AlbumNameLabel</textfield>";
	content+="<textfield name='albumName' html='true' x='80'>"+albumName+"</textfield>";
	content+="<textfield name='albumOperLabel' x='460' y='0' width='200' selectable='no' html='true'>"+AlbumOperLabel+"</textfield>";
	content+="<hotspot name='albumCreateSpot' x='460' y='0' alpha='0' width='90' height='20'/>";
	content+="<hotspot name='albumListSpot' x='555' y='0' alpha='0' width='80' height='20'/>";
	
	content+=spec;

	content+="<handler ins='BookSpecial-chapterListInfo' type='onMLInit' value='BookSpecial' src='lib:notes.components.content.bookPage.BookSpecial.onChapterListInit'/>";
	content+="</portlet></moonlight>";
	
	return content;
}

function appendOper(y:Number):String
{
	var content="";
	content+="<textfield name='chapterOperLabel' x='400' y='"+y+"' width='270' selectable='no' html='true'>ChapterOperLabel</textfield>";
	content+="<hotspot name='chapterUploadSpot' x='400' y='"+y+"' alpha='0' width='60' height='20'/>";
	content+="<hotspot name='chapterRecordSpot' x='465' y='"+y+"' alpha='0' width='60' height='20'/>";
	content+="<hotspot name='chapterReportSpot' x='530' y='"+y+"' alpha='0' width='60' height='20'/>";
	content+="<hotspot name='chapterPlaySpot' x='595' y='"+y+"' alpha='0' width='50' height='20'/>";
	content+="<textfield name='chapterOperLabel' x='338' y='"+Number(y+25)+"' width='320' selectable='no' html='true'>ChapterPlayLabel</textfield>";
	content+="<hotspot name='textViewSpot' x='338' y='"+Number(y+25)+"' alpha='0' width='60' height='20'/>";
	content+="<hotspot name='textDownloadSpot' x='400' y='"+Number(y+25)+"' alpha='0' width='60' height='20'/>";
	content+="<hotspot name='audioPlaySpot' x='465' y='"+Number(y+25)+"' alpha='0' width='60' height='20'/>";
	content+="<hotspot name='audioDownloadSpot' x='530' y='"+Number(y+25)+"' alpha='0' width='60' height='20'/>";
	content+="<hotspot name='mobileDownloadSpot' x='595' y='"+Number(y+25)+"' alpha='0' width='50' height='20'/>";
	return content;
}